var laydate, table, form, miniTab;
var projectTable;
layui.use(['form', 'table', 'laydate', 'miniTab'], function () {
    form = layui.form,
        table = layui.table,
        laydate = layui.laydate,
        miniTab = layui.miniTab;

    //日期范围
    laydate.render({
        elem: '#date-range'
        , range: true
    });
    //全局设置table、
    table.set({
        request: {
            pageName: 'currentPage', //页码的参数名称，默认：page
            limitName: 'pageSize',
        },
        response: {
            statusName: 'code' //数据状态的字段名称，默认：code
            , statusCode: '000000' //成功的状态码，默认：0
            , msgName: 'msg' //状态信息的字段名称，默认：msg
            , countName: 'total' //数据总数的字段名称，默认：count
            , dataName: 'rows' //数据列表的字段名称，默认：data
        },
        limits: [10, 15, 20, 25, 50, 100],
        limit: 10,
        page: true
    });
    projectList.init();
});

var projectList = {
    init: function () {
        // 监听回车
        ad.keydown.enter('search');
        // 初始化
        projectList.initTable.load();

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            projectList.reload();
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                layer.open({
                    title: '新增项目',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: baseUrl + 'project/add',
                    end: function () {
                        projectList.reload();
                        layer.msg('处理成功');
                    }
                });
            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'view') {
                miniTab.toTab({
                    tabId: 'project_to_file' + data.projectId,
                    href: baseUrl + 'file/list?projectId=' + data.projectId,
                    title: '文件管理-' + data.project,
                    maxTabNum: '20'
                });
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    layer.close(index);
                    globalObj.delProject(data);
                });
            } else if (obj.event === 'zip') {
                globalObj.zipByProject(data);
            }
        });
    },

    initTable: {
        load: function() {
            if (projectTable) {
                projectList.reload();
            }
            projectTable = table.render({
                elem: '#currentTableId',
                url: baseUrl + 'project/getList',
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print'],
                cols: [[
                    {field: 'project', title: '项目名称'},
                    {field: 'town', title: '所属村镇'},
                    {field: 'projectDate', title: '立项日期', templet: "#projectDate"},
                    {field: 'projectType', title: '项目类别'},
                    {title: '操作', width: 200, toolbar: '#currentTableBar', fixed: "right", align: "center"}
                ]],
                where: ad.common.formToJson('projectForm')
            });
        }
    },

    reload: function() {
        table.reload('currentTableId', {
            where: ad.common.formToJson('projectForm')
        });
    }
};

var globalObj = globalObj || {};
globalObj = $.extend(globalObj, {
    delProject: function (data) {
        (new globalObj.Request(globalObj.url)).setParams({
            param: {
                projectId: data.projectId
            },
            serviceId: "app.project.delete",
        }).do().success(function (data) {
            projectList.reload();
            layer.msg('处理成功');
        });
    },
    zipByProject: function (data) {
        (new globalObj.Request(globalObj.url)).setParams({
            param: {
                projectId: data.projectId
            },
            serviceId: "app.file.zipByProject"
        }).do().success(function (data) {
            // 下载返回地址
            downloadZip(data);
        });
    }
});

var downloadZip = function (url) {
    var $form = $('<form method="GET"></form>');
    $form.attr('action', url);
    $form.appendTo($('body'));
    $form.submit();
};